package com.ross.web.transaction;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.ross.web.DTO.ProductDTO;
import com.ross.web.dao.ProductDAO;
import com.ross.web.dao.ReturnProductDAO;
import com.ross.web.domain.Product;
import com.ross.web.domain.ReturnProduct;
import com.ross.web.util.DateUtil;
import com.ross.web.util.SessionLogout;

public class ReturnProductsAction extends ActionSupport {
	
	private TreeMap<String,ProductDTO> productDTOList = new TreeMap<String,ProductDTO>();
	private List<ReturnProduct> soldProductsList;
	private int dispMode;
	private Product product;
	private String grandTotal;
	private ProductDTO productDTO = new ProductDTO();
	private String barcode;
	private int quantity;
	private String remarks;
	private ReturnProduct returnproduct;
	private int rp_id;
	private List<ProductDTO> productList = new ArrayList<ProductDTO>();
	
	SessionLogout sessionLogout = new SessionLogout();
	HttpServletRequest request = ServletActionContext.getRequest();
	
	ReturnProductDAO returnProductDAO = new ReturnProductDAO();
	ProductDAO productDAO = new ProductDAO();
	
	public String goToReturnProductsAction(){
		
		if (sessionLogout.SessionExpired()) {
			addActionMessage("Session already expired");
			return "sessionExpired";
		}
		
		setDispMode(1);
		soldProductsList = returnProductDAO.getAllReturnProducts();
		
		return SUCCESS;
		
	}
	
	public String goToAddReturnProductsAction(){
		
		if (sessionLogout.SessionExpired()) {
			addActionMessage("Session already expired");
			return "sessionExpired";
		}
		
		setGrandTotal(null);
		setProductDTOList(null);
		request.getSession().setAttribute("productDTOList",null);
		setDispMode(2);
		return SUCCESS;
	}
	
	public String addToReturnCartAction(){
		
		if (sessionLogout.SessionExpired()) {
			addActionMessage("Session already expired");
			return "sessionExpired";
		}
		
		try{
			
			product = productDAO.getProductByBarCode(getBarcode());
			
			if(product == null){
				addActionError(getText("error.sellproduct.notexist"));
				productDTOList = (TreeMap<String, ProductDTO>) request.getSession().getAttribute("productDTOList");
				getTotal();
				return ERROR;
				
			}
			
			productDTO.setBarcode(product.getBarcode());
			productDTO.setName(product.getName());
			productDTO.setPrice(product.getPrice());
			productDTO.setQuantity(1);
				
			if(request.getSession().getAttribute("productDTOList") == null){
				productDTOList.put(productDTO.getName()+productDTO.getBarcode(),productDTO);
				request.getSession().setAttribute("productDTOList", productDTOList);
			}else{
				productDTOList = (TreeMap<String, ProductDTO>) request.getSession().getAttribute("productDTOList");
				ProductDTO prodDTO = productDTOList.get(productDTO.getName()+productDTO.getBarcode());
					if(prodDTO == null){
						productDTOList.put(productDTO.getName()+productDTO.getBarcode(), productDTO);
					}else{
						productDTO.setQuantity(prodDTO.getQuantity() + productDTO.getQuantity());
						productDTOList.put(productDTO.getName()+productDTO.getBarcode(),productDTO);
					}
					
				request.getSession().setAttribute("productDTOList", productDTOList);
			}
				getTotal();
			
		}catch(Exception e){
			e.printStackTrace();
		}
		
		
		setDispMode(2);
		return SUCCESS;
		
		
	}
	
	public String changeReturnQuantityAction(){
		
		if (sessionLogout.SessionExpired()) {
			addActionMessage("Session already expired");
			return "sessionExpired";
		}
		
		try {
			
			productDTOList = (TreeMap<String, ProductDTO>) request.getSession().getAttribute("productDTOList");
			if(quantity == 0){
				Product p = productDAO.getProductByBarCode(getBarcode());
				productDTOList.remove(p.getName()+getBarcode());
			}else{
			Product p = productDAO.getProductByBarCode(getBarcode());
			productDTO = productDTOList.get(p.getName()+getBarcode());
			productDTO.setQuantity(quantity);
			
			productDTOList.put(productDTO.getName()+getBarcode(), productDTO);
			
			}
			request.getSession().setAttribute("productDTOList", productDTOList);
			
			getTotal();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		setDispMode(2);
		return SUCCESS;
	}
	
	public String doSaveReturnLogAction(){
		
		if (sessionLogout.SessionExpired()) {
			addActionMessage("Session already expired");
			return "sessionExpired";
		}
		
		try {
			
			productDTOList = (TreeMap<String, ProductDTO>) request.getSession().getAttribute("productDTOList");
			String productsReturned = "";
			Collection<ProductDTO> col = productDTOList.values();
			Iterator<ProductDTO> it = col.iterator();
			ProductDTO productDTOx;
			while(it.hasNext()){
				productDTOx = it.next();
				if(productDTOx.getBarcode() != ""){
					productsReturned+= "-" + productDTOx.getName() + "-" + productDTOx.getQuantity() + "-" + productDTOx.getTotalPriceString();
				}
			}
			
			ReturnProduct rp = new ReturnProduct();
			rp.setProductids(productsReturned);
			rp.setReturndate(DateUtil.getDateToday());
			rp.setReturntime(DateUtil.getTimeToday());
			rp.setTotalvalue(getTotal());
			rp.setRemarks(getRemarks());
			
			returnProductDAO.insert(rp);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		setGrandTotal(null);
		setProductDTOList(null);
		request.getSession().setAttribute("productDTOList",null);
		soldProductsList = returnProductDAO.getAllReturnProducts();
		setDispMode(1);
		return SUCCESS;
		
	}
	
	public String goToViewReturnProductAction(){
		
		if (sessionLogout.SessionExpired()) {
			addActionMessage("Session already expired");
			return "sessionExpired";
		}
		
		try {
			
			returnproduct = returnProductDAO.getReturnProductById(rp_id);
			String[] products = returnproduct.getProductids().split("-");
			for(int a=1; a < products.length;a+=3){
				try {
					ProductDTO productDTO = new ProductDTO();
					productDTO.setName(products[a]);
					productDTO.setQuantity(Integer.valueOf(products[a+1]));
					productDTO.setSubTotal(products[a+2]);
					System.out.println(productDTO.getName());
					productList.add(productDTO);
				} catch (IndexOutOfBoundsException e) {
					break;
				}
			}
				
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		setDispMode(3);
		return SUCCESS;
	}

	private String getTotal(){
	
		double total = 0;
		Collection<ProductDTO> col = productDTOList.values();
		Iterator<ProductDTO> it = col.iterator();
		
		ProductDTO productDTOx;
		while(it.hasNext()){
			productDTOx = it.next();
			if(productDTOx.getBarcode() != ""){
			total += productDTOx.getTotalPrice();
			}
		}
		grandTotal = String.format("%.2f", total);
	
	return grandTotal;
}
	
	public String resetReturnAction(){
		if (sessionLogout.SessionExpired()) {
			addActionMessage("Session already expired");
			return "sessionExpired";
		}
		
			
		setGrandTotal(null);
		setProductDTOList(null);
		request.getSession().setAttribute("productDTOList",null);
			
		
		setDispMode(2);
		return SUCCESS;
	}
	
	

	public ReturnProduct getReturnproduct() {
		return returnproduct;
	}

	public void setReturnproduct(ReturnProduct returnproduct) {
		this.returnproduct = returnproduct;
	}

	public int getRp_id() {
		return rp_id;
	}

	public void setRp_id(int rp_id) {
		this.rp_id = rp_id;
	}

	public List<ProductDTO> getProductList() {
		return productList;
	}

	public void setProductList(List<ProductDTO> productList) {
		this.productList = productList;
	}

	public String getRemarks() {
		return remarks;
	}

	public void setRemarks(String remarks) {
		this.remarks = remarks;
	}

	public int getQuantity() {
		return quantity;
	}

	public void setQuantity(int quantity) {
		this.quantity = quantity;
	}

	public TreeMap<String, ProductDTO> getProductDTOList() {
		return productDTOList;
	}

	public void setProductDTOList(TreeMap<String, ProductDTO> productDTOList) {
		this.productDTOList = productDTOList;
	}

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	public String getGrandTotal() {
		return grandTotal;
	}

	public void setGrandTotal(String grandTotal) {
		this.grandTotal = grandTotal;
	}

	public ProductDTO getProductDTO() {
		return productDTO;
	}

	public void setProductDTO(ProductDTO productDTO) {
		this.productDTO = productDTO;
	}

	public String getBarcode() {
		return barcode;
	}

	public void setBarcode(String barcode) {
		this.barcode = barcode;
	}

	public int getDispMode() {
		return dispMode;
	}

	public void setDispMode(int dispMode) {
		this.dispMode = dispMode;
	}

	public List<ReturnProduct> getSoldProductsList() {
		return soldProductsList;
	}

	public void setSoldProductsList(List<ReturnProduct> soldProductsList) {
		this.soldProductsList = soldProductsList;
	}
	
	

}
